package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LocalTest {
	/**
	 * without file transfer test.
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String host=System.getProperty("url","jdbc:laud://localhost:9170?data_port=9091&support_function=true&hive=false");
		Class.forName("cn.edu.thu.laud.jdbc.LaUDDriver");
		Connection con = DriverManager.getConnection(host, "testadmin", "test");
		Statement statement=con.createStatement();
		try{
			statement.execute("drop keyspace  Keyspace2 ;");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		try{
			statement.execute("create keyspace  Keyspace2 with  strategy_class ='org.apache.cassandra.locator.SimpleStrategy' and strategy_options:replication_factor=1;");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		statement.execute("use Keyspace2");
		try{
			statement.execute("create columnfamily Standard1 (id varchar primary key,age int,name varchar,nick varchar,imagefeature blob,sphoto 'sblob')");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
				try{
					statement.execute("create index user1_nick_idx on Standard1(nick)"); 
				}catch (SQLException e) {
					System.err.println(e.getMessage());
				}
				try{
					statement.execute("create index user1_age_idx on Standard1(age) "); 
					
				}catch (SQLException e) {
					System.err.println(e.getMessage());
				}
		
	
		
		try{
			statement.execute("create index  on Standard1(name) "); 	
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		ResultSet rsResultSet;

	//	statement.execute("delete from Standard1 where id=1");


		
		statement.execute("insert into Standard1(id,name,age) values (4,'yiguolei',23)"); //,'F:/lib/laud-thrift-1.0.7-1.jar'

		//for(int i=0;i<100;i++){
			statement.execute("insert into Standard1(id,name,sphoto) values (101,'yiguolei5','e:/test.pdf')"); //,'F:/lib/laud-thrift-1.0.7-1.jar'
			rsResultSet = statement.getResultSet();
			while(rsResultSet.next()){
				rsResultSet.updateBlob("hphoto", System.in);
				//rsResultSet.updateBlob("sphoto", System.in);
				//rsResultSet.updateBlob("lphoto", System.in);
			}
			rsResultSet.close();
		//}


		statement.execute("select * from Standard1 where id=101");
		rsResultSet = statement.getResultSet();
		int i=0;
		while(rsResultSet.next()){
			System.out.println(rsResultSet.getObject(1));
			System.out.println(rsResultSet.getObject(2));
			
//			System.out.println(rsResultSet.getObject("id"));
//			System.out.println(rsResultSet.getObject("name"));
//			//hblob ,lblob .we will save them automaticly..
//			//rsResultSet.getBlob("hphoto");
//			//rsResultSet.getBlob("lphoto");
//
//			//((FileTransfor)rsResultSet).getBlob("lphoto","g:/result/");
//			((FileTransfor)rsResultSet).getBlob("hphoto","g:/result/"+i);
//			//System.out.println("lphoto:"+rsResultSet.getString("lphoto"));
//			System.out.println("hphoto:"+rsResultSet.getString("hphoto"));
//			/*
//					//if we want to save in a designed location.we should do like this:
//					 ((FileTransfer)rsResultSet).getBlob("photo","e:/test/");
//			 */
//			//with sblob,we do not save them in file . we return a java.sql.Blob object.
//
////			Blob blob=rsResultSet.getBlob("sphoto");
////			//we can save this blob as a file like this:
////			if(blob!=null){
////				InputStream iStream=blob.getBinaryStream();
////				writeIntoFile(iStream, "g:/result/abc"+i);
////			}
			i++;
			System.out.println("------------------------------");
		}
		//remeber close ResultSet !!!!!!
		rsResultSet.close();
		statement.close();
	}
}
